package com.ym.health.api.common;

import com.ym.common.constant.Constants;
import com.ym.common.enums.SmsTemplateEnum;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.medical.domain.param.VerificationCodeParam;
import com.ym.medical.enums.SmsCodeTypeEnum;
import com.ym.medical.service.IMessageCodeSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 验证码相关
 *
 * @author: 李涛
 * @version: 2020年08月17日 16:56
 */

@Api(tags = "api-验证码相关")
@RestController
@RequestMapping(value = "/common/sms", headers = Constants.Api.VERSION)
@Validated
public class MessageCodeController extends BaseController {

	@Autowired
	private IMessageCodeSV messageCodeSV;

	@Log(dataBase = false, printBody = false)
	@ApiOperation("获取手机号验证码")
	@GetMapping("/code")
	public APIResponse code(
			@RequestParam String phone,
			@RequestParam SmsCodeTypeEnum codeType,
			@RequestParam SmsTemplateEnum tempType
	) {
		String code = messageCodeSV.sentCode(phone, codeType, tempType);
		return successMsg(code);
	}

	@Log
	@ApiOperation("主动验证验证码")
	@PostMapping("/verificationCode")
	public APIResponse verificationCode(
			@RequestBody @Valid VerificationCodeParam param
	) {
		boolean ok = messageCodeSV.validatePhoneCode(param.getPhone(), param.getCodeType(), param.getTempType(), param.getCode());
		return successData(ok);
	}
}
